<style>
.pl30{
    padding-left: 30px;
}
.mb20{
    margin-bottom: 20px;
}
</style>
<template>
    <div>
        <product-title></product-title>
        <div class="wrap pl30">
            <!-- <Circle :percent="percent" :stroke-color="color">
                <Icon v-if="percent == 100" type="ios-checkmark-empty" size="60" style="color:#5cb85c"></Icon>
                <span v-else style="font-size:24px">{{ percent }}%</span>
            </Circle> -->
            <Form  :label-width="160">
                <FormItem label="同步员工数据：">
                    <Button type="primary" :disabled='canclick||(progress != 0 && progress != 100)' @click="syncuserinfo">同步员工数据</Button>
                </FormItem>
                <!-- <FormItem label="更新员工基本信息：">
                    <Button type="primary" :disabled='canclick' @click="updateBaseinfo">更新基本信息</Button>
                </FormItem> -->
                <!-- <FormItem label="更新员工FYC：">
                        <Row>
                            <Col span="3" >
                                <DatePicker type="month" :options="options" placeholder="选择更新月份" @on-change="dateChange($event)" v-model="fycdatetime"></DatePicker>
                            </Col>
                            <Col span="3" offset="1">
                                <Button type="primary" :disabled='canclick' @click="updateFYC">更新员工FYC</Button>
                            </Col>
                        </Row>
                    
                </FormItem>
                <FormItem label="计算员工奖金：">
                        <Row>
                            <Col span="3" >
                                <DatePicker type="month" :options="options" placeholder="选择更新月份" @on-change="perdateChange($event)" v-model="Persistencydatetime"></DatePicker>
                            </Col>
                            <Col span="3" offset="1">
                                <Button type="primary" :disabled='canclick' @click="updatebonus">计算员工奖金</Button>
                            </Col>
                        </Row>
                    
                </FormItem> -->
                <FormItem label="计算员工工资：">
                        <Row>
                            <Col span="3" >
                                <DatePicker type="month" :options="options" placeholder="选择更新月份" @on-change="salaryChange($event)" v-model="salarydatetime"></DatePicker>
                            </Col>
                            <Col span="3" offset="1">
                                <Button type="primary" :disabled='canclick' @click="updatesalary">计算员工工资</Button>
                            </Col>
                        </Row>
                </FormItem>
                <!-- <FormItem label="导出员工工资表：">
                        <Row>
                            <Col span="3" >
                                <DatePicker type="month" :options="options" placeholder="选择导出月份" @on-change="exportChange($event)" v-model="exportdatetime"></DatePicker>
                            </Col>
                            <Col span="3" offset="1">
                                <Button type="success" :disabled='canclick' @click="exportsalary">导出员工工资表</Button>
                            </Col>
                        </Row>
                </FormItem> -->
            </Form>
            <h3 class="mb20">
                更新详情:
            </h3>
             {{synctxt}}
            <p v-show="progress != 0">
               
                {{progress != 100 ? '同步员工数据:同步中……'+progress+'%' : '同步员工数据：已完成！'}}
            </p>
            <p>
                {{baseinfotxt}}
            </p>
            <p>
                {{fyctxt}}
            </p>
            <p>
                {{bonus}}
            </p>
            <p>
                {{salary}}
            </p>
            <!-- <p>
                导出员工工资表:   <a href="http://w.kuaibao365.com/excel/export_salary">员工工资表.xls</a>
            </p> -->
        </div>
        
    </div>
</template>
<script>
import productTitle from '../../components/productTitle'
import { getAllUid  , getAllUidRank , getAllId , getAllIdInvert , synchData} from '../../api/employee'
import { updateBasicInfo ,  updatePersistency , setCheckData , getCheck} from '../../api/common'
import { updateInfo , computeCommission , computeBonus , computeSalary } from '../../api/settlement'
import { formatTime } from '../../libs/time'
export default {
    data () {
        return {
            options: {
                disabledDate (date) {
                    let _date = formatTime(new Date(),'yyyy-MM')
                    return date && date.valueOf() >= (new Date(_date).getTime() - 86400000)
                }
            },
            userlistasc:[],
            userlist:[],
            userlistrank:[],
            tagindex:0,
            current:0,
            canclick:false,
            fycdate:'',
            fycdatetime:'',
            Persistencydate:'',
            Persistencydatetime:'',
            salarydate:'',
            salarydatetime:'',
            exportdate:'',
            exportdatetime:'',
            synctxt:'',
            baseinfotxt:'',
            fyctxt:'',
            pertxt:'',
            bonus:'',
            salary:'',
            checktxt:'',
            candown:false,
            progress:0
        }
    },
    computed: {
        percent() {
            return this.$store.state.socket.synch_data ? this.$store.state.socket.synch_data : 0
        }
    },
    components: {
        productTitle,
        
    },
    channel: 'progress.'+sessionStorage.getItem('id'),
    mounted() {

        console.log(this.$echo)
        let _id = 'progress.'+sessionStorage.getItem('id')
        this.$echo.channel(_id).listen('ProgreeNotice', (payload) => {
            console.log(payload);
            this.$nextTick(() => {
                setTimeout(() => {
                    this.progress = payload.progress
                },200)
            })
             this.$store.dispatch('changesocket', payload);
        });
    },
    created () {
        this.getUid()
        let _time = new Date(formatTime(new Date(),'yyyy-MM')).getTime() - 86400000
        let _date =formatTime(new Date(_time),'yyyy-MM')
        this.fycdate = _date
        this.fycdatetime = _date
        this.Persistencydate = _date
        this.Persistencydatetime = _date
        this.salarydate = _date
        this.salarydatetime = _date
    },

    methods: {
        exportChange(date){
            this.exportdate = date
        },
        //导出某月员工工资
        exportsalary(){

        },
        //点击同步员工数据
        syncuserinfo(){
            let _userlistasc = this.userlistasc
            console.log(_userlistasc)
            if(_userlistasc.length > 0){
                this.canclick = true
                this.syncinfo(_userlistasc)
            }else{
                this.$Message.error('员工数据异常');
            }
        },
        //同步员工数据
        syncinfo(_userlistasc){
            
            new Promise((resolve, reject) => {
                synchData().then(response => {
                    if(response.data.err == 0){
                        this.canclick = false
                        this.synctxt = ''
                    } else {
                        this.synctxt = '同步员工数据：操作失败，请重试。'
                        this.current = 0
                        this.canclick = false
                        this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    this.canclick = false
                    reject(error);
                });
            });
        },
        dateChange(date){
            this.fycdate = date
        },
        perdateChange(date){
            this.Persistencydate =date
        },
        salaryChange(date){
            this.salarydate = date
        },
        //获取全部员工
        getUid(){           
            new Promise((resolve, reject) => {
                getAllId().then(response => {
                    if(response.data.err == 0){
                        this.userlistasc = response.data.data
                    } else {
                       this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
            new Promise((resolve, reject) => {
                getAllIdInvert().then(response => {
                    if(response.data.err == 0){
                        this.userlist = response.data.data
                    } else {
                       this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
            // new Promise((resolve, reject) => {
            //     getAllUidRank().then(response => {
            //         if(response.data.err == 0){
            //             this.userlistrank = response.data.data
            //         } else {
            //            this.$Message.error(response.data.info);
            //         }                       
            //         resolve();
            //     }).catch(error => {
            //         reject(error);
            //     });
            // });
            
        },
        //点击更新基本信息
        updateBaseinfo(){
            let _userlist = this.userlist
            if(_userlist.length > 0){
                this.canclick = true
                this.updateinfo(_userlist)
            }else{
                this.$Message.error('员工数据异常');
            }
        },
        //更新基本信息
        updateinfo(_userlist){
            let _length = _userlist.length
            if(this.current >=_userlist.length){
                this.baseinfotxt = '更新基本信息：已完成全部更新'
                this.current = 0
                this.canclick = false
                return;  
            }
            let _params = {
                eid : _userlist[this.current]    
            }
            new Promise((resolve, reject) => {
                updateInfo(_params).then(response => {
                    if(response.data.err == 0){
                        this.baseinfotxt = '更新基本信息：'+this.current+'/'+_length+'已完成更新'
                        this.current++
                        this.updateinfo(_userlist)

                    } else {
                        this.canclick = false
                        this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    this.canclick = false
                    reject(error);
                });
            });
        },
        //点击更新员工FYC
        updateFYC(){
            let _userlist = this.userlist
            if(_userlist.length > 0){
                if(!this.fycdate){
                    this.$Message.error('请选择年月')
                     
                }else{
                    this.canclick = true
                    this.updateuserfyc(_userlist)
                }
                
            }else{
                this.$Message.error('员工数据异常');
            }
        },
        //更新fyc
        updateuserfyc(_userlist){
            let _length = _userlist.length
            if(this.current >=_userlist.length){
                this.fyctxt = '更新员工FYC：已完成全部更新'
                this.current = 0
                this.canclick = false
                return;  
            }
            
            let _params = {
                uid : _userlist[this.current],
                date: this.fycdate
            }
            //console.log(_params)
            new Promise((resolve, reject) => {
                computeCommission(_params).then(response => {
                    console.log(response.data)
                    if(response.data.err == 0){
                        //this.prolist = response.data.data
                        this.fyctxt = '更新员工FYC：'+this.current+'/'+_length+'已完成更新'
                        this.current++
                        this.updateuserfyc(_userlist)

                    } else {
                        this.canclick = false
                        this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    this.canclick = false
                    reject(error);
                });
            });
        },

        //点击计算员工奖金
        updatebonus(){
            let _userlistrank = this.userlistrank
            console.log(_userlistrank)
            if(_userlistrank.length > 0){                
                if(!this.Persistencydate){
                    this.$Message.error('请选择年月')
                     
                }else{
                    this.canclick = true
                    this.updatebonusdata(_userlistrank)
                }
            }else{
                this.$Message.error('员工数据异常');
            }
        },
        updatebonusdata(_userlistrank){
            let _Length = _userlistrank.length
            if(this.current < _Length){
                this.bonus = '计算员工奖金：'+this.current+'/'+_Length+'已完成更新'
                let _ranklist = _userlistrank[this.current]
                //console.log(_ranklist)
                //this.rankupdata(_ranklist)
                let _rank= _ranklist.rank
                let _arry = _ranklist.data
                console.log(_arry.length)
                let _length = _arry.length
                if(_length > 0){
                    this.rankajax(_rank,_arry,_length)
                }
            }else{
                this.bonus = '计算员工奖金：已完成全部更新'
                this.current = 0
                this.tagindex = 0
                this.canclick = false
                return 
            }
        },
        rankajax(_rank,_arry,_length){
            let _params = {
                date: this.Persistencydate,
                uid:_arry[this.tagindex]
            }
            let _len = 0
            new Promise((resolve, reject) => {
                computeBonus(_params).then(response => {
                    console.log(response.data)
                    if(response.data.err == 0){
                        this.tagindex ++
                        if(this.tagindex < _length){
                            this.rankajax(_rank,_arry,_length)
                        }
                        else if(this.tagindex == _length){
                            this.current ++
                            this.tagindex = 0
                            this.updatebonusdata(this.userlistrank)
                        } else{
                            return 
                        } 
                    } else {
                        this.current = 0
                        this.canclick = false
                        this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    this.canclick = false
                    reject(error);
                });
            });
        },
        //点击计算员工工资
        updatesalary(){
            let _userlist = this.userlist
            console.log(_userlist)
            if(_userlist.length > 0){
                if(!this.salarydate){
                    this.$Message.error('请选择年月')                     
                }else{
                    this.canclick = true
                    this.updatesalaryinfo(_userlist)
                }
            }else{
                this.$Message.error('员工数据异常');
            }
        },
        //计算员工工资
        updatesalaryinfo(_userlistasc){
            let _length = _userlistasc.length
            if(this.current >=_userlistasc.length){
                this.salary = '计算员工工资：已完成全部更新'
                this.candown = true
                this.current = 0
                this.canclick = false
                return;  
            }
            let _params = {
                date: this.salarydate,
                eid : _userlistasc[this.current]    
            }
            new Promise((resolve, reject) => {
                computeSalary(_params).then(response => {
                    if(response.data.err == 0){
                        this.salary = '计算员工工资：'+this.current+'/'+_length+'已完成更新'
                        this.current++
                        this.updatesalaryinfo(_userlistasc)

                    } else {
                        this.salary = '计算员工工资：操作失败，请重试。'
                        this.current = 0
                        this.canclick = false
                        this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    this.canclick = false
                    reject(error);
                });
            });
        },

        //点击计算员工工资
        // updatesalary(){
        //     let _userlistrank = this.userlistrank
        //     console.log(_userlistrank)
        //     if(_userlistrank.length > 0){                
        //         if(!this.salarydate){
        //             this.$Message.error('请选择年月')
                     
        //         }else{
        //             this.canclick = true
        //             this.updatesalarydata(_userlistrank)
        //         }
        //     }else{
        //         this.$Message.error('员工数据异常');
        //     }
        // },
        // updatesalarydata(_userlistrank){
        //     let _Length = _userlistrank.length
        //     if(this.current < _Length){
        //         this.salary = '计算员工工资：'+this.current+'/'+_Length+'已完成更新'
        //         let _ranklist = _userlistrank[this.current]
        //         //console.log(_ranklist)
        //         //this.rankupdata(_ranklist)
        //         let _rank= _ranklist.rank
        //         let _arry = _ranklist.data
        //         console.log(_arry.length)
        //         let _length = _arry.length
        //         if(_length > 0){
        //             this.salaryajax(_rank,_arry,_length)
        //         }
        //     }else{
        //         this.salary = '计算员工工资：已完成全部更新'
        //         this.current = 0
        //         this.tagindex = 0
        //         this.canclick = false
        //         return 
        //     }
        // },
        // salaryajax(_rank,_arry,_length){
        //     let _params = {
        //         date: this.salarydate,
        //         uid:_arry[this.tagindex]
        //     }
        //     let _len = 0
        //     new Promise((resolve, reject) => {
        //         computeSalary(_params).then(response => {
        //             console.log(response.data)
        //             if(response.data.err == 0){
        //                 this.tagindex ++
        //                 if(this.tagindex < _length){
        //                     this.salaryajax(_rank,_arry,_length)
        //                 }
        //                 else if(this.tagindex == _length){
        //                     this.current ++
        //                     this.tagindex = 0
        //                     this.updatesalarydata(this.userlistrank)
        //                 } else{
        //                     return 
        //                 } 
        //             } else {
        //                 this.current = 0
        //                 this.canclick = false
        //                 this.$Message.error(response.data.info);
        //             }                       
        //             resolve();
        //         }).catch(error => {
        //             this.canclick = false
        //             reject(error);
        //         });
        //     });
        // }
    }
}
</script>

